const sql = require("../sql");
const user = require("../sql/user")
const md5 = require("md5");
const jwt = require("jsonwebtoken");

module.exports = (req, res)=>{
    const {token, oldpw, password} = req.body;

    // 验证token是否失效
    jwt.verify(token, "杨树林er", (err, data)=>{
        if(err){
            res.send({code:0, msg:"登录失效"});
        }else{
            // 根据用户名，修改用户信息
            sql.get({
                colObj: user,
                where: {
                    username: data.username,
                    password: md5(oldpw)
                }
            }).then(result=>{
                if(result.length > 0){
                    sql.set(user, {username: data.username}, {
                        password: md5(password)
                    }).then(()=>{
                        res.send({code:1, msg:"修改成功"});
                    })
                }else{
                    res.send({code:2, msg:"原密码错误"});
                }
            })
        }
    })

}
